package com.cloudeasy.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudeasy.server.mapper.RolePermissionMapper;
import com.cloudeasy.server.po.Permission;
import com.cloudeasy.server.po.RolePermission;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RolePermissionService extends ServiceImpl<RolePermissionMapper, RolePermission> {
    private final PermissionService permissionService;

    public RolePermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    /**
     * 获取角色权限记录
     *
     * @param roleIds 角色Id列表
     * @return {@link List<RolePermission>}
     */
    public List<RolePermission> list(List<String> roleIds) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roleIds);
        return this.list(queryWrapper);
    }

    /**
     * 获取可用权限
     *
     * @param roleIds 角色Id列表
     * @return {@link List<Permission>}
     */
    public List<Permission> getEnablePermission(List<String> roleIds) {
        List<String> permissionIds = this.list(roleIds).stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
        if (permissionIds.size() == 0) {
            return null;
        }
        return permissionService.list(permissionIds);
    }
}
